//
//  CActionSheet.m
//  GoldOperationClient
//
//  Created by yaxue wang on 16/8/9.
//  Copyright © 2016年 展鸿科技. All rights reserved.
//

#import "CActionSheet.h"

#define CActionSheetHeight 47
#define CActionOtherHeight 6

@interface CActionSheet()
@property (nonatomic, strong) NSMutableArray *sheets;
@property (nonatomic, strong) NSString *title;
@property (nonatomic, strong) NSString *cancel;
@end

@implementation CActionSheet

- (instancetype)initWithTitle:(NSString *)title cancel:(NSString *)cancel{
    return [self initWithTitle:title cancel:cancel other:nil];
}

- (instancetype)initWithTitle:(NSString *)title cancel:(NSString *)cancel other:(NSArray *)other{
    self = [super initWithFrame:[UIScreen mainScreen].bounds];
    if (self) {
        _title = title;
        _cancel = cancel;
        _sheets = [NSMutableArray array];
    }
    return self;
}

- (void) addSheet:(NSString *)title{
    [_sheets addObject:@{@"title":title}];
}

- (void) addSheet:(NSString *)title withBlock:(void(^)()) block{
    [_sheets addObject:@{@"title":title, @"block":block}];
}

- (void) showInView:(UIView *)view{
    [view addSubview:self];
    
    for (UIView *subview in [self subviews]) {
        [subview removeFromSuperview];
    }
    
    UIView *bgView = [[UIView alloc] init];
    [self addSubview:bgView];
    [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self);
    }];
    [bgView setBackgroundColor:[UIColor colorWithRed:0 green:0 blue:0 alpha:.6]];
    [bgView setUserInteractionEnabled:YES];
    
    int more = (_title ? 1 : 0) + (_cancel ? 1 : 0);
    float height = (more + _sheets.count) * CActionSheetHeight + more * CActionOtherHeight;
    
    UIView *sheetView = [[UIView alloc]initWithFrame:CGRectMake(0, view.frame.size.height, view.frame.size.width, height)];
    [self addSubview:sheetView];
    
    void (^remove)() = ^(){
        [UIView animateWithDuration:.25 animations:^{
            CGRect frame = sheetView.frame;
            frame.origin.y = self.frame.size.height;
            [sheetView setFrame:frame];
        } completion:^(BOOL finished) {
            if (finished) {
                [self removeFromSuperview];
            }
        }];
    };
    
    [bgView bk_whenTapped:^{
        remove();
    }];
    
    UILabel *lblTitle = [[UILabel alloc] init];
    [sheetView addSubview:lblTitle];
    [lblTitle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.equalTo(_title ? @CActionSheetHeight : @0);
        make.left.and.right.with.top.equalTo(sheetView);
    }];
    [lblTitle setFont:F(18)];
    [lblTitle setTextAlignment:NSTextAlignmentCenter];
    [lblTitle setHidden:YES];
    [lblTitle setBackgroundColor:[UIColor whiteColor]];
    if (_title) {
        [lblTitle setHidden:NO];
        [lblTitle setText:_title];
    }
    
    UIImage *imgClose = [UIImage imageNamed:@"Ico_close"];
    UIButton *btnClose = [[UIButton alloc] init];
    [sheetView addSubview:btnClose];
    [btnClose mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(sheetView).mas_offset(15);
        make.centerY.equalTo(lblTitle);
        make.size.mas_equalTo(imgClose.size);
    }];
    [btnClose setImage:imgClose forState:UIControlStateNormal];
    [btnClose setHidden:YES];
    [btnClose bk_addEventHandler:^(id sender) {
        remove();
    } forControlEvents:UIControlEventTouchUpInside];
    if (_title) {
        [btnClose setHidden:NO];
    }
    
    int i = 0;
    for (NSDictionary *sheet in _sheets) {
        UILabel *lblsheet = [[UILabel alloc] init];
        [sheetView addSubview:lblsheet];
        [lblsheet mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.and.right.equalTo(sheetView);
            make.height.equalTo(@CActionSheetHeight);
            make.top.equalTo(lblTitle.mas_bottom).mas_offset(i * CActionSheetHeight + (_title ? CActionOtherHeight : 0) + i * .5);
        }];
        [lblsheet setBackgroundColor:[UIColor whiteColor]];
        [lblsheet setFont:F(16)];
        lblsheet.textColor =[UIColor blackColor];
        [lblsheet setTextAlignment:NSTextAlignmentCenter];
        [lblsheet setUserInteractionEnabled:YES];
        [lblsheet setText:sheet[@"title"]];
        [lblsheet bk_whenTapped:^{
            remove();
            if (sheet[@"block"]) {
                void (^sheetBlock)() = sheet[@"block"];
                sheetBlock();
            }else if(self.CSheetSelected){
                self.CSheetSelected(i);
            }
        }];
      [lblsheet setNumberOfLines:2];
        i ++;
    }
    
    UILabel *lblCancel = [[UILabel alloc] init];
    [sheetView addSubview:lblCancel];
    [lblCancel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.equalTo(@CActionSheetHeight);
        make.left.and.right.with.bottom.equalTo(sheetView);
    }];
    [lblCancel setFont:F(16)];
    [lblCancel setTextAlignment:NSTextAlignmentCenter];
    [lblCancel setHidden:YES];
    [lblCancel setUserInteractionEnabled:YES];
    [lblCancel setBackgroundColor:[UIColor whiteColor]];
    if (_cancel) {
        [lblCancel setHidden:NO];
        [lblCancel setText:_cancel];
        [lblCancel bk_whenTapped:^{
            remove();
            if (self.CSheetSelected) {
                self.CSheetSelected(-1);
            }
        }];
    }
    
    [UIView animateWithDuration:.25 animations:^{
        CGRect frame = sheetView.frame;
        frame.origin.y = self.frame.size.height - height;
        [sheetView setFrame:frame];
    }];
}


@end
